//
//  ChartFileHelper.m
//  Pods
//
//  Created by E.F.B on 15-5-5.
//
//

#import "ChartFileHelper.h"

@implementation ChartFileHelper

+ (NSString *)pathForChartsDB:(NSString *)revisionID
{
    NSArray * dirs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * cachesPath = [dirs objectAtIndex:0];
    NSString * storagePath = [cachesPath stringByAppendingPathComponent:@"charts"];
    storagePath=[storagePath stringByAppendingPathComponent:revisionID];
    
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:storagePath]) {
        return [NSString stringWithFormat:@"%@/tc.db3",storagePath];
    }
    return [NSString stringWithFormat:@"%@/tc.db3",storagePath];
}

+ (NSString *)pathForChartsPlist:(NSString *)revisionID userName:(NSString *)name
{
    static NSString * rootDir = @"charts";
    
    NSArray * dirs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * cachesPath = [dirs objectAtIndex:0];
    NSString * storagePath = [cachesPath stringByAppendingPathComponent:rootDir];
    
    storagePath=[storagePath stringByAppendingPathComponent:revisionID];
    
    if (name.length>0) {
        
        storagePath=[storagePath stringByAppendingPathComponent:name];
        
        NSFileManager *manager=[NSFileManager defaultManager];
        if (![manager fileExistsAtPath:storagePath]) {
            [manager createDirectoryAtPath:storagePath withIntermediateDirectories:YES attributes:nil error:nil];
        }
    }
    
    return storagePath;
}

+ (NSString*)pathForCharts:(NSString *)task_id
{
    NSString* fileName = @"";
    if (task_id.length > 0) {
        fileName = [NSString stringWithFormat:@"/%@.zip", task_id];
    }
    
    NSString* folderPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/charts/"];
    
    NSFileManager *_manager=[NSFileManager defaultManager];
    if (![_manager fileExistsAtPath:folderPath]) {
        [_manager createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    return [folderPath stringByAppendingString:fileName];
}

+ (NSString*)pathForChartsPlist
{
    NSString* folderPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/charts/"];
    
    NSFileManager *_manager=[NSFileManager defaultManager];
    if (![_manager fileExistsAtPath:folderPath]) {
        [_manager createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    return [folderPath stringByAppendingString:@"/chartsPage.plist"];
}


@end
